在現代行動應用開發中,安全性是至關重要的考量。使用者的隱私資料需要得到妥善保護,而生物辨識技術提供了一種既安全又便利的身分驗證方式。今天我們將探討如何在 Tauri Android 應用中整合生物辨識功能。
傳統的密碼驗證方式存在許多缺點:容易被遺忘、可能被盜取、輸入繁瑣等。相較之下,生物辨識具有以下優勢:
tauri-plugin-biometric
一如既往,只要簡單的一個指令就可以安裝了:
npm run tauri add biometric
在 Vue 前端中,我們可以透過插件提供的 JavaScript API 來檢查是否支援生物辨識流程:
import { checkStatus } from '@tauri-apps/plugin-biometric';
const status = await checkStatus();
if (status.isAvailable) {
console.log('Yes! Biometric Authentication is available');
} else {
console.log(
'No! Biometric Authentication is not available due to ' + status.error
);
}
也可以在 Rust:
use tauri_plugin_biometric::BiometricExt;
fn check_biometric(app_handle: tauri::AppHandle) {
let status = app_handle.biometric().status().unwrap();
if status.is_available {
println!("Yes! Biometric Authentication is available");
} else {
println!("No! Biometric Authentication is not available due to: {}", status.error.unwrap());
}
}
如果有支援,就可以開始驗證:
import { authenticate } from '@tauri-apps/plugin-biometric';
const options = {
// Set true if you want the user to be able to authenticate using phone password
allowDeviceCredential: false,
cancelTitle: "Feature won't work if Canceled",
// iOS only feature
fallbackTitle: 'Sorry, authentication failed',
// Android only features
title: 'Tauri feature',
subtitle: 'Authenticate to access the locked Tauri function',
confirmationRequired: true,
};
try {
await authenticate('This feature is locked', options);
console.log(
'Hooray! Successfully Authenticated! We can now perform the locked Tauri function!'
);
} catch (err) {
console.log('Oh no! Authentication failed because ' + err.message);
}
use tauri_plugin_biometric::{BiometricExt, AuthOptions};
fn bio_auth(app_handle: tauri::AppHandle) {
let options = AuthOptions {
// Set True if you want the user to be able to authenticate using phone password
allow_device_credential:false,
cancel_title: Some("Feature won't work if Canceled".to_string()),
// iOS only feature
fallback_title: Some("Sorry, authentication failed".to_string()),
// Android only features
title: Some("Tauri feature".to_string()),
subtitle: Some("Authenticate to access the locked Tauri function".to_string()),
confirmation_required: Some(true),
};
// if the authentication was successful, the function returns Result::Ok()
// otherwise returns Result::Error()
match app_handle.biometric().authenticate("This feature is locked".to_string(), options) {
Ok(_) => {
println!("Hooray! Successfully Authenticated! We can now perform the locked Tauri function!");
}
Err(e) => {
println!("Oh no! Authentication failed because : {e}");
}
}
}
生物辨識技術適用於多種應用場景:
在整合生物辨識功能時,需要特別注意使用者體驗:
首先,應提供清晰的提示訊息,讓使用者了解為何需要進行生物辨識。其次,必須提供替代驗證方式,因為某些情況下生物辨識可能無法使用(如手指受傷、光線不足等)。
最後,應尊重使用者的選擇權,允許他們決定是否啟用生物辨識功能。
透過 tauri-plugin-biometric
,我們可以輕鬆在 Tauri Android 應用中實現專業級的生物辨識功能。這不僅提升了應用的安全性,也大幅改善了使用者體驗。